<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="adminList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="name" align='center' :label="$t('管理员名称')" min-width="160"></el-table-column>
      <el-table-column prop="role" align='center' :label="$t('角色')" min-width="160">
        <template slot-scope="scope">
          {{ $t(scope.row.role) }}
        </template>
      </el-table-column>
      <el-table-column prop="banned" align='center' :formatter="bannedFormat" :label="$t('是否锁定')" min-width="160"></el-table-column>
      <el-table-column prop="ukey" align='center' :formatter="ukeyFormat" :label="$t('是否启用多鉴别')" min-width="160"></el-table-column>
      <el-table-column align='center' :width="lang === 'zh'?'350':'400'" :label="$t('操作')" fixed="right">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row, 'edit')" :class="'edit' + (scope.$index + 1)">{{$t('编辑')}}</el-button>
          <el-button type="danger" v-if="!scope.row.banned" plain size="mini" @click="bannedClick(scope.row, 'enable', '锁定')" :class="'close' + (scope.$index + 1)">{{$t('锁定')}}</el-button>
          <el-button type="success" v-else plain size="mini" @click="bannedClick(scope.row, 'disable', '解锁')" :class="'open' + (scope.$index + 1)">{{$t('解锁')}}</el-button>
          <el-button type="success" plain v-if="!scope.row.ukey" size="mini" @click="keyClick(scope.row, 'enable', '启用')" :class="'start' + (scope.$index + 1)">{{$t('启用UKEY')}}</el-button>
          <el-button type="danger" plain v-else size="mini" @click="keyClick(scope.row, 'disable', '停用')" :class="'stop' + (scope.$index + 1)">{{$t('停用UKEY')}}</el-button>
          <el-button type="success" plain size="mini" @click="bingUkey(scope.row)" :class="'bingUkey' + (scope.$index + 1)">{{$t('绑定UKEY')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <AdminConfigDialog v-if="adminConfigDialogVisible" ref="refAdminConfigDialogVisible"></AdminConfigDialog>
  </div>
</template>
<script>

import { getAdminArray, delAdministrators, editUkey, editBanned, getRandomKey } from '@/api/system/adminConfig/administrators'
import AdminConfigDialog from './components/AdminConfigDialog'
export default {
  name: 'AdminConfig',
  components: {
    AdminConfigDialog
  },
  data () {
    return {
      adminConfigDialogVisible: false,
      lang: this.$i18n.locale,
      adminList: [],
      nameStr: '',
      statusList: { 'on': '启用', 'off': '禁用' },
      levelList:
      {
        1: 'ERROR',
        2: 'ALERT',
        3: 'INFO',
        4: 'DEBUG'
      }

    }
  },
  created () {
    this.listData()
  },
  methods: {
    ukeyFormat (data) {
      return data.ukey ? this.$t('已启用') : this.$t('未启用')
    },
    bannedFormat (data) {
      return data.banned ? this.$t('已锁定') : this.$t('未锁定')
    },
    bannedClick (data, status, name) {
      this.$confirm(this.$t('确定' + name + '吗？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        editBanned({ name: data.name, status: status }).then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t(res.msg),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    },
    keyClick (data, status, name) {
      this.$confirm(this.$t('确定' + name + '吗？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        editUkey({ name: data.name, status: status }).then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t(res.msg),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    },
    // 多选按钮
    handleSelectionChange (data) {
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.name
        } else {
          nameStr = nameStr + '@#@' + item.name
        }
      }
      this.nameStr = nameStr
    },
    // 列表
    listData () {
      getAdminArray().then(res => {
        if (res.status === 0) {
          this.adminList = res.info
        }
      })
    },
    // 添加按钮
    dialogVisible (data, type) {
      this.adminConfigDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refAdminConfigDialogVisible.init(data, type)
      })
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delAdministrators({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$messages.success(this.$t('删除成功'))
              this.nameStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    bingUkey (row) {
      this.OnLoadWrite().then(res => {
        getRandomKey().then(res => {
          if (res.status === 0) {
            this.Arm_Enum_Write([res.info.key, res.info.pub, row.name])
          }
        })
      })
    }
  }
}
</script>
